package yns.springboot.common.sample.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yns.springboot.common.sample.base.bean.BaseDto;
import yns.springboot.common.sample.base.bean.BaseEntity;
import yns.springboot.common.sample.base.service.impl.BaseServiceImpl;
import yns.springboot.common.sample.dao.DataInfoDao;
import yns.springboot.common.sample.dto.DataInfoDto;
import yns.springboot.common.sample.entity.DataInfoEntity;
import yns.springboot.common.sample.page.PaginationInfo;
import yns.springboot.common.sample.page.PaginationSearchInfo;
import yns.springboot.common.sample.service.DataInfoService;

@Service
public class DataInfoServiceImpl extends BaseServiceImpl implements DataInfoService {

	@Autowired
	private DataInfoDao dataInfoDao;

	@Override
	public List<DataInfoDto> getDataInfo() {
		List<DataInfoDto> listDataInfoDto = new ArrayList<DataInfoDto>();
		List<DataInfoEntity> listDataInfoEntity = dataInfoDao.getDataInfo();
		
		listEntityToDto(listDataInfoEntity, listDataInfoDto);
		
		return listDataInfoDto;
	}

	@Override
	public int addDataInfo(DataInfoDto dataInfoDto) {
		DataInfoEntity dataInfoEntity = new DataInfoEntity();
		transformDtoToEntity.parseObject(dataInfoDto, dataInfoEntity);

		return dataInfoDao.addDataInfo(dataInfoEntity);
	}

	@Override
	public DataInfoDto getDataInfoByID(int id) {
		DataInfoEntity dataInfoEntity = dataInfoDao.getDataInfoByID(id);
		DataInfoDto dataInfoDto = new DataInfoDto();
		transformEntityToDto.parseObject(dataInfoEntity, dataInfoDto);

		return dataInfoDto;
	}

	@Override
	public int updateDataInfo(DataInfoDto dataInfoDto) {
		DataInfoEntity dataInfoEntity = new DataInfoEntity();
		transformDtoToEntity.parseObject(dataInfoDto, dataInfoEntity);

		return dataInfoDao.updateDataInfo(dataInfoEntity);
	}

	@Override
	public int getTotalCount() {
		return dataInfoDao.getTotalCount();
	}

	@Override
	public List<DataInfoDto> getPage(int currentPage, int pageSize) {

		PaginationInfo<DataInfoEntity> paginationInfo = new PaginationInfo<DataInfoEntity>();
		paginationInfo.setCurrentPage(currentPage);
		paginationInfo.setPageSize(pageSize);

		PaginationInfo<DataInfoEntity> pagingResults = pagination.paging(dataInfoDao, paginationInfo);
		List<DataInfoEntity> listDataInfoEntity = pagingResults.getDataList();
		
		List<DataInfoDto> listDataInfoDto = new ArrayList<DataInfoDto>();
		
		listEntityToDto(listDataInfoEntity, listDataInfoDto);
		
		
		return listDataInfoDto;
	}

	@Override
	public void listEntityToDto(List<DataInfoEntity> listEntity, List<DataInfoDto> listDto) {
		
		DataInfoDto dto;
		for (DataInfoEntity entity : listEntity) {
			dto = new DataInfoDto();
			transformEntityToDto.parseObject(entity, dto);
			listDto.add(dto);
		}
		
	}

	@Override
	public void listDtoToEntity(List<DataInfoDto> listDto, List<DataInfoEntity> listEntity) {
		
		DataInfoEntity entity;
		for (DataInfoDto dto : listDto) {
			entity = new DataInfoEntity();
			transformDtoToEntity.parseObject(dto,entity);
			listEntity.add(entity);
		}
		
	}

	@Override
	public List<DataInfoDto> search(int currentPage,int pageSize,DataInfoDto dto) {
		
		PaginationSearchInfo<DataInfoEntity> paginationSearchInfo = new PaginationSearchInfo<DataInfoEntity>();
		paginationSearchInfo.setCurrentPage(currentPage);
		paginationSearchInfo.setPageSize(pageSize);

		PaginationSearchInfo<DataInfoEntity> pagingResults = pagination.search(dataInfoDao, paginationSearchInfo);
		List<DataInfoEntity> listDataInfoEntity = pagingResults.getDataList();
		
		List<DataInfoDto> listDataInfoDto = new ArrayList<DataInfoDto>();
		
		listEntityToDto(listDataInfoEntity, listDataInfoDto);
		
		
		return listDataInfoDto;
	}




}
